package org.iori.luodan.logic;

import java.util.HashMap;
import org.iori.luodan.base.BaseDaoFactory;
import org.iori.luodan.dao.UserDao;
import org.iori.luodan.entity.UserInfoEntity;
import com.iori.common.log.FounderLog;
import com.iori.common.log.LogFactory;
import com.iori.common.util.MD5Util;

/**
 * [クラス名]<br>
 * UserLogic.java<br>
 * [機能概要]<br>
 * 検索ロジック<br>
 * <br>
 * [変更履歴]<br>
 * 2012/10/10 ver1.00 新規作成<br>
 * 
 * @author luodan CORPORATION lun_xing
 * @version 1.00
 */
public class UserLogic {

    // Log出力クラスを定義
    private static FounderLog log = LogFactory.getFounderLog(UserLogic.class);

    // DAO初期化
    private static UserDao userDao = BaseDaoFactory.getUserDao();

    /**
     * コンテンツリストの内容取得
     * <br>
     * @param offset
     * @param limit
     * @param TitleName
     * @return
     * @throws Exception
     */
    public UserInfoEntity getUserInfo(String user_name, String password, Boolean isMd5) throws Exception {
    	if (null == user_name || null == password) {
    		return null;
    	}
    	
    	String str_password = password;
    	if (true == isMd5) {
    		str_password = MD5Util.getMD5(str_password);
    	}

    	HashMap map = userDao.getUserInfo(user_name, str_password);

		UserInfoEntity userInfoEntity = this.mapToEntity(map);

		return userInfoEntity;
    }
    
    private UserInfoEntity mapToEntity(HashMap map) {
    	if (null == map) {
    		return null;
    	}
    	Object obj = null;
    	obj = map.get("USER_ID");
    	int user_id = 0;
    	if (null != obj) {
    		user_id = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("EMAIL");
    	String email = "";
    	if (null != obj) {
    		email = obj.toString().trim();
    	}
    	
    	obj = map.get("USER_NAME");
    	String user_name = "";
    	if (null != obj) {
    		user_name = obj.toString().trim();
    	}
    	
    	obj = map.get("PASSWORD");
    	String password = "";
    	if (null != obj) {
    		password = obj.toString().trim();
    	}

    	obj = map.get("QUESTION");
    	String question = "";
    	if (null != obj) {
    		question = obj.toString().trim();
    	}
    	
    	obj = map.get("ANSWER");
    	String answer = "";
    	if (null != obj) {
    		answer = obj.toString().trim();
    	}
    	
    	obj = map.get("SEX");
    	Boolean sex = false;
    	if (null != obj) {
    		sex = Boolean.valueOf(obj.toString().trim());
    	}

    	obj = map.get("BIRTHDAY");
    	String birthday = "";
    	if (null != obj) {
    		birthday = obj.toString().trim();
    	}

    	obj = map.get("USER_MONEY");
    	float user_money = 0.0f;
    	if (null != obj) {
    		user_money = Float.valueOf(obj.toString().trim());
    	}

    	obj = map.get("FROZEN_MONEY");
    	float frozen_money = 0.0f;
    	if (null != obj) {
    		frozen_money = Float.valueOf(obj.toString().trim());
    	}

    	obj = map.get("PAY_POINTS");
    	int pay_points = 0;
    	if (null != obj) {
    		pay_points = Integer.valueOf(obj.toString().trim());
    	}

    	obj = map.get("RANK_POINTS");
    	int rank_points = 0;
    	if (null != obj) {
    		rank_points = Integer.valueOf(obj.toString().trim());
    	}

    	obj = map.get("ADDRESS_ID");
    	int address_id = 0;
    	if (null != obj) {
    		address_id = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("REG_TIME");
    	int reg_time = 0;
    	if (null != obj) {
    		reg_time = Integer.valueOf(obj.toString().trim());
    	}

    	obj = map.get("LAST_LOGIN");
    	int last_login = 0;
    	if (null != obj) {
    		last_login = Integer.valueOf(obj.toString().trim());
    	}

		obj = map.get("LAST_TIME");
		String last_time = "";
		if (null != obj) {
			last_time = obj.toString().trim();
		}
		
		obj = map.get("LAST_IP");
		String last_ip = "";
		if (null != obj) {
			last_ip = obj.toString().trim();
		}
		
    	obj = map.get("VISIT_COUNT");
    	int visit_count = 0;
    	if (null != obj) {
    		visit_count = Integer.valueOf(obj.toString().trim());
    	}

    	obj = map.get("USER_RANK");
    	int user_rank = 0;
    	if (null != obj) {
    		user_rank = Integer.valueOf(obj.toString().trim());
    	}

    	obj = map.get("IS_SPECIAL");
    	int is_special = 0;
    	if (null != obj) {
    		is_special = Integer.valueOf(obj.toString().trim());
    	}
    	
		obj = map.get("SALT");
		String salt = "";
		if (null != obj) {
			salt = obj.toString().trim();
		}

    	obj = map.get("PARENT_ID");
    	int parent_id = 0;
    	if (null != obj) {
    		parent_id = Integer.valueOf(obj.toString().trim());
    	}

    	obj = map.get("FLAG");
    	int flag = 0;
    	if (null != obj) {
    		flag = Integer.valueOf(obj.toString().trim());
    	}

		obj = map.get("ALIAS");
		String alias = "";
		if (null != obj) {
			alias = obj.toString().trim();
		}

		obj = map.get("MSN");
		String msn = "";
		if (null != obj) {
			msn = obj.toString().trim();
		}
		
		obj = map.get("QQ");
		String qq = "";
		if (null != obj) {
			qq = obj.toString().trim();
		}
		
		obj = map.get("OFFICE_PHONE");
		String office_phone = "";
		if (null != obj) {
			office_phone = obj.toString().trim();
		}
		
		obj = map.get("HOME_PHONE");
		String home_phone = "";
		if (null != obj) {
			home_phone = obj.toString().trim();
		}
		
		obj = map.get("MOBILE_PHONE");
		String mobile_phone = "";
		if (null != obj) {
			mobile_phone = obj.toString().trim();
		}

    	obj = map.get("IS_VALIDATED");
    	int is_validated = 0;
    	if (null != obj) {
    		is_validated = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("CREDIT_LINE");
    	float credit_line = 0.0f;
    	if (null != obj) {
    		credit_line = Float.valueOf(obj.toString().trim());
    	}

		obj = map.get("PASSWD_QUESTION");
		String passwd_question = "";
		if (null != obj) {
			passwd_question = obj.toString().trim();
		}
		
		obj = map.get("PASSWD_ANSWER");
		String passwd_answer = "";
		if (null != obj) {
			passwd_answer = obj.toString().trim();
		}

		UserInfoEntity userInfoEntity = new UserInfoEntity();

		userInfoEntity.setUser_id(user_id);
		userInfoEntity.setEmail(email);
		userInfoEntity.setUser_name(user_name);
		userInfoEntity.setPassword(password);
		userInfoEntity.setQuestion(question);
		userInfoEntity.setAnswer(answer);
		userInfoEntity.setSex(sex);
		userInfoEntity.setBirthday(birthday);
		userInfoEntity.setUser_money(user_money);
		userInfoEntity.setFrozen_money(frozen_money);
		userInfoEntity.setPay_points(pay_points);
		userInfoEntity.setRank_points(rank_points);
		userInfoEntity.setAddress_id(address_id);
		userInfoEntity.setReg_time(reg_time);
		userInfoEntity.setLast_login(last_login);
		userInfoEntity.setLast_time(last_time);
		userInfoEntity.setLast_ip(last_ip);
		userInfoEntity.setVisit_count(visit_count);
		userInfoEntity.setUser_rank(user_rank);
		userInfoEntity.setIs_special(is_special);
		userInfoEntity.setSalt(salt);
		userInfoEntity.setParent_id(parent_id);
		userInfoEntity.setFlag(flag);
		userInfoEntity.setAlias(alias);
		userInfoEntity.setMsn(msn);
		userInfoEntity.setQq(qq);
		userInfoEntity.setOffice_phone(office_phone);
		userInfoEntity.setHome_phone(home_phone);
		userInfoEntity.setMobile_phone(mobile_phone);
		userInfoEntity.setIs_validated(is_validated);
		userInfoEntity.setCredit_line(credit_line);
		userInfoEntity.setPasswd_question(passwd_question);
		userInfoEntity.setPasswd_answer(passwd_answer);

    	return userInfoEntity;
    }
}
